package com.imc.api.order.controller;

import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.Lesson;
import com.imc.api.order.pojo.OrderBillDto;
import com.imc.api.order.pojo.SecCondDto;
import com.imc.api.order.service.IOrderService;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 类描述：订单控制类
 * 开发者：YT
 * 创建日期：2018/10/28 21:09
 */
@Slf4j
@Controller
@RequestMapping("/api/public/wx/order")
public class OrderController {

    @Autowired
    IOrderService orderService;//订单服务类

    @Autowired
    MysqlUtil mysqlUtil;//数据库操作类

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    /**
     * QY 2018/9/13 0:12
     * 方法描述：获取所有学校
     **/
    @RequestMapping(value = "/querySchoolList", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO querySchoolList(
            @RequestParam(defaultValue = "0") String longitude,
            @RequestParam(defaultValue = "0")String latitude) {
        ResultDTO resultDTO = new ResultDTO();
        List <Map <String, Object>> retList = orderService.querySchoolList(longitude,latitude);
        if (retList.size() > 0) {
            resultDTO.setIsSuccess(true);
            resultDTO.setMessage("查询成功");
            resultDTO.setCode("40001");
            resultDTO.setData(retList);
            return resultDTO;
        }
        resultDTO.setIsSuccess(false);
        resultDTO.setMessage("查询学校失败");
        resultDTO.setCode("40002");
        return resultDTO;
    }

    /**
     * QY 2018/9/13 0:11
     * 方法描述：根据学校ID获取课程
     **/
    @RequestMapping(value = "/queryCategoryListBySchoolId", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryCourseListBySchoolId(@RequestParam(defaultValue = "") String schoolId, @RequestParam(defaultValue = "1") String cateType) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
//                    PageInfo info = new PageInfo <>(orderService.queryCategoryListBySchoolId(schoolId, cateType,pageable));
                    t.setData(orderService.queryCategoryListBySchoolId(schoolId, cateType));
                    t.setIsSuccess(true);
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage(e.getMessage());
                    e.printStackTrace();
                    log.error("error:", e.toString());
                }
        );
    }

    /**
     * QY 2018/9/20 17:48
     * 方法描述：根据类型查课程
     **/
    @RequestMapping(value = "/queryLessonByCategoryId", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryLessonByCategoryId(String keyword, @RequestParam(defaultValue = "") String categoryId, @RequestParam String schoolId) {
        return ResultDTO.builder().data(orderService.queryLessonByCategoryId(keyword, categoryId, schoolId)).build();
    }

    /**
     * QY 2018/9/13 10:43
     * 方法描述：查询筛选条件
     **/
    @RequestMapping(value = "/queryScrCond", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryScrCond() {
        return ResultDTO.builder().data(orderService.queryScrCond()).build();
    }

    /**
     * QY 2018/9/13 11:04
     * 方法描述：根据条件筛选查询
     **/
    @RequestMapping(value = "/queryCourseBySecCond", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryCourseBySecCond(@RequestBody SecCondDto secCondDto, @PageableDefault Pageable pageable) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            PageInfo <Lesson> result = orderService.queryCourseBySecCond(secCondDto, pageable);
            resultDTO.setIsSuccess(true);
            resultDTO.setData(
                    new MyPageImpl <>(result.getList(), result.isIsLastPage())
            );
            resultDTO.setMessage("查询成功");
        } catch (Exception e) {
            resultDTO.setIsSuccess(false);
            resultDTO.setMessage(e.getMessage());
        }
        return resultDTO;
    }

    /**
     * QY 2018/9/17 19:46
     * 方法描述：根据课程期数查询课程详情
     **/
    @RequestMapping(value = "/queryDetailsByLessOnId", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryDetailsByLessOnId(@RequestParam String lessonId, @RequestParam(defaultValue = "") String schoolId) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            resultDTO.setData(orderService.queryDetailsByLessOnId(lessonId, schoolId));
            resultDTO.setIsSuccess(true);
            resultDTO.setMessage("查询成功");
        } catch (Exception e) {
            resultDTO.setIsSuccess(false);
            resultDTO.setMessage(e.getMessage());
        }
        return resultDTO;
    }


    @GetMapping("/queryMealAll")
    @ResponseBody
    public  ResultDTO queryMealAll(@RequestParam(defaultValue = "1") String SchoolId){
        return iComboExecptionForTFunc
                .executeFlow(
                        ResultDTO.builder().build(),
                        t -> {
                           t.setData(orderService.queryMealAll(SchoolId));
                           t.setIsSuccess(true);
                           t.setMessage("获取成功");
                           t.setCode("200");
                        },
                        (e, t) -> {
                            t.setCode("201");
                            e.printStackTrace();
                            t.setIsSuccess(false);
                            t.setMessage("获取详情失败");
                        }
                );
    }

}